package teamproject.visualization.gui.visualobjects;

import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.RenderingAttributes;
import javax.vecmath.Color3f;

import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;

public class EdgeNotUsed implements VisualObject {

	private float length = 0;
	private int parts = 0;
	private float radius = 0;
	private ArrayList<CustomCylinder> listOfParts = new ArrayList<CustomCylinder>();
	private Appearance app;
	private RenderingAttributes ra;
	private Color3f color;

	public EdgeNotUsed(float length, int numberOfParts) {
		this(length, numberOfParts, 1.0f);
	}

	public EdgeNotUsed(float length, int numberOfParts, float radius) {
		this.setLength(length);
		this.setNumberOfParts(numberOfParts);
		this.setRadius(radius);
		ra = new RenderingAttributes();
		ra.setCapability(RenderingAttributes.ALLOW_VISIBLE_WRITE);
		app = createAppearance();

		for (int i = 0; i < parts; i++) {
			listOfParts
					.add(new CustomCylinder(radius, (length / parts),
							Primitive.GENERATE_NORMALS, app, i));
		}
	}

	private Appearance createAppearance() {
		Appearance appear = new Appearance();

		// colour
		appear = new Appearance();
		ColoringAttributes caColor = new ColoringAttributes();
		caColor.setColor(0.988f, 0.400f, 0.080f);
		appear.setColoringAttributes(caColor);

		// Light properties
		color = new Color3f(0.988f, 0.400f, 0.080f);
		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
		appear.setMaterial(new Material(color, black, color, black, 80.0f));

		appear.setRenderingAttributes(ra);

		return appear;
	}

	public Color3f getColor() {
		return color;
	}

	public float getLength() {
		return length;
	}

	public int getNumberOfParts() {
		return parts;
	}

	public ArrayList<CustomCylinder> getParts() {
		return listOfParts;
	}

	public float getRadius() {
		return radius;
	}

	@Override
	public boolean isVisible() {
		return ra.getVisible();
	}

	public void setColor(Color3f color) {
		this.color = color;

		for (CustomCylinder cyl : listOfParts) {
			cyl.getAppearance().getMaterial().setAmbientColor(color);
			cyl.getAppearance().getMaterial()
					.setEmissiveColor(new Color3f(0.0f, 0.0f, 0.0f));
			cyl.getAppearance().getMaterial().setDiffuseColor(color);
			cyl.getAppearance().getMaterial()
					.setSpecularColor(new Color3f(0.0f, 0.0f, 0.0f));
			cyl.getAppearance().getMaterial().setShininess(80.0f);
		}
	}

	public void setLength(float length) {
		this.length = length;
	}

	public void setNumberOfParts(int parts) {
		this.parts = parts;
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	@Override
	public void setVisible(boolean visible) {
		ra.setVisible(visible);
	}
}